{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7df0a440",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import pyreadr\n",
    "from __future__ import division\n",
    "import pylab as pl\n",
    "from matplotlib.backends.backend_pdf import PdfPages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1f5808b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "brain_signals = pyreadr.read_r(\"filtered_signals.Rdata\")\n",
    "muscle_signals = pyreadr.read_r(\"filtered_muscle_signals.Rdata\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a20598c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_hbtot_filt_pass = list(brain_signals.items())[1][1]\n",
    "df_hboxy_filt_pass = list(brain_signals.items())[3][1]\n",
    "\n",
    "df_hbtot_filt_pass_muscle = list(muscle_signals.items())[1][1]\n",
    "df_hboxy_filt_pass_muscle = list(muscle_signals.items())[3][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "123e49cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f_plot_psd(df, num):\n",
    "    \n",
    "    data = df[df[\"number\"] == num]\n",
    "    sampling_rate = 1/0.02\n",
    "    \n",
    "    fourier_transform = np.fft.rfft(data[\"sg_filt\"])\n",
    "    abs_fourier_transform = np.abs(fourier_transform)\n",
    "    power_spectrum = np.square(abs_fourier_transform)\n",
    "    frequency = np.linspace(0, sampling_rate/2, len(power_spectrum))\n",
    "    \n",
    "    psd = {\"freq\":frequency, \"power\":power_spectrum}\n",
    "    df_psd = pd.DataFrame(psd)\n",
    "    df_plot = df_psd[df_psd[\"freq\"] < 25]\n",
    "    df_plot = df_plot[df_plot[\"freq\"] > 0.01]\n",
    "    \n",
    "    fig = plt.figure()  \n",
    "    plt.plot(df_plot[\"freq\"], df_plot[\"power\"], color = 'k')\n",
    "    plt.axvline(x = 0.15, color = 'r', linestyle = '--')\n",
    "    plt.xticks(np.arange(0, 25, 1))\n",
    "    \n",
    "    return fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f193ebe",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# TM0003CM01\n",
    "\n",
    "p_cm_brain_hbtot = f_plot_psd(df_hbtot_filt_pass, 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e75ce253",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_cm_muscle_hbtot = f_plot_psd(df_hbtot_filt_pass_muscle, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a247eff",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_cm_brain_hboxy = f_plot_psd(df_hboxy_filt_pass, 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58319213",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_cm_muscle_hboxy = f_plot_psd(df_hboxy_filt_pass_muscle, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8592cf8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# TM2003HV01\n",
    "\n",
    "p_hv_brain_hbtot = f_plot_psd(df_hbtot_filt_pass, 70)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75acd61a",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_hv_muscle_hbtot = f_plot_psd(df_hbtot_filt_pass_muscle, 88)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "763b2a60",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_hv_brain_hboxy = f_plot_psd(df_hboxy_filt_pass, 70)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbfe16bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_hv_muscle_hboxy = f_plot_psd(df_hboxy_filt_pass_muscle, 88)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a9750c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# TM1003UM01\n",
    "\n",
    "p_um_brain_hbtot = f_plot_psd(df_hbtot_filt_pass, 50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5094800",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_um_muscle_hbtot = f_plot_psd(df_hbtot_filt_pass_muscle, 49)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c7dc706",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_um_brain_hboxy = f_plot_psd(df_hboxy_filt_pass, 50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8875d011",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "p_um_muscle_hboxy = f_plot_psd(df_hboxy_filt_pass_muscle, 49)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "0942c75d",
   "metadata": {},
   "outputs": [],
   "source": [
    "pp = PdfPages(\"Outputs/psd_plots.pdf\")\n",
    "\n",
    "pp.savefig(p_cm_brain_hbtot)\n",
    "pp.savefig(p_cm_muscle_hbtot)\n",
    "pp.savefig(p_cm_brain_hboxy)\n",
    "pp.savefig(p_cm_muscle_hboxy)\n",
    "pp.savefig(p_hv_brain_hbtot)\n",
    "pp.savefig(p_hv_muscle_hbtot)\n",
    "pp.savefig(p_hv_brain_hboxy)\n",
    "pp.savefig(p_hv_muscle_hboxy)\n",
    "pp.savefig(p_um_brain_hbtot)\n",
    "pp.savefig(p_um_muscle_hbtot)\n",
    "pp.savefig(p_um_brain_hboxy)\n",
    "pp.savefig(p_um_muscle_hboxy)\n",
    "\n",
    "pp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f09ea6f1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
